/**
 * Created by jinxs on 14-10-16.
 */
var listUrl = '../data/list.json';
var saveUrl = 'add.html';//保存添加数据地址
var loadFormUrl = 'formData.json';//加载表单数据地址
var dialogSize = {width: 500, height: 360};//弹出窗口大小
var deleteUrl = 'delete.html';//删除地址
$(function () {
    //初始化grid
    var grid = $('#grid').uiGrid({
        autoFit: true,
        border: false,
        colFit: true,
        colFitAllTime: false,
        gridTitleFix: true,
        showCellTip: false,
        showHeader: false,
        icon: 'list',
        url: listUrl
    });
    //初始化搜索框
    $('#searchbox').uiSearchbox({
        prompt: '按姓名、用户名',
        searcher: function (v) {
            grid.uiGrid('loadData', {queryParams: {key: $.trim(v)}});
        }
    });
    //初始化表单
    var formInit = function () {
        $('#grid_form').uiForm({
            template:'grid_form_temp',
            onLoadSuccess: function () {
                var txtBirth = $('#txtBirth');
                var selSex = $('#sel');
                var selStatus=$('#selStatus');
                var selNation=$('#selNation')
                switch ($('#dialog').uiDialog('getUserParam')) {
                    case 'view':
                        txtBirth.uiDate();
                        selSex.uiCombobox();
                        selStatus.uiCombobox();
                        $('#grid_form').uiForm('setReadonly', true);
                        break;
                    case 'edit':
                        txtBirth.uiDate({disabled: true});
                        selSex.uiCombobox();
                        selStatus.uiCombobox();
                        break;
                    case 'add':
                        selNation.uiCombobox();
                        txtBirth.uiDate();
                        selSex.uiCombobox();
                        selStatus.uiCombobox();
                        break;
                    default :
                        break;
                }
            }
        });
    };
    //初始化弹出窗口
    var dialog = $('#dialog').uiDialog({
        title: '新建',
        icon: 'list',
        width: dialogSize.width,
        height: dialogSize.height,
        mask: true,
        showBtnBar: true,
        lazyLoad: true,
        url: 'edit.html',
        onLoadComplete: formInit,
        onOpen:function(){
            var form=$('#grid_form');
            switch ($('#dialog').uiDialog('getUserParam')) {
                case 'view':
                    //form.uiForm('load', loadFormUrl);//加载远程数据
                    form.uiForm('load', $('#grid').uiGrid('selectedData')[0]);//加载客户端数据
                    break;
                case 'edit':
                    //form.uiForm('load', loadFormUrl);//加载远程数据
                    form.uiForm('load', $('#grid').uiGrid('selectedData')[0]);//加载客户端数据
                    break;
                case 'add':
                    form.uiForm('load');
                    break;
                default :
                    break;
            }
        },
        buttons: [
            {
                id: 'btnOK',
                label: '提交',
                style: 'button_submit',
                icons: {left: 'icon_add'},
                disabled: false,
                onClick: function () {
                    $('#grid_form').uiForm('submit', {
                        url: saveUrl,
                        onSubmit: function () {
                            var isValid = $('#grid_form').uiForm('validate');
                            if (!isValid) {
                            }
                            return isValid;	// 返回false终止表单提交
                        },
                        success: function (data) {
                            dialog.uiDialog('close');
                            showResult(true, '提交成功！');
                        }
                    });
                    return false;
                }
            },
            {
                id: 'btnSave',
                label: '保存',
                style: 'button_submit',
                icons: {left: 'icon_edit'},
                disabled: false,
                onClick: function () {
                    var ids = grid.uiGrid('selectedData', 'id');
                    $('#grid_form').uiForm('submit', {
                        url: saveUrl,
                        onSubmit: function (params) {
                            params.id = ids[0];
                            var isValid = $('#grid_form').uiForm('validate');
                            if (!isValid) {
                            }
                            return isValid;	// 返回false终止表单提交
                        },
                        success: function (data) {
                            dialog.uiDialog('close');
                            showResult(true, '保存成功！');
                        }
                    });
                    return false;
                }
            },
            {
                id: 'btnCancel',
                label: '取消',
                icons: {left: 'icon_cancel'},
                disabled: false,
                style: 'button_cancel',
                onClick: function () {
                    dialog.uiDialog('close');
                }
            }
        ]
    });
    //新建
    $('#btn1').bind('click', function () {
        dialog.uiDialog('title').html('新建');
        dialog.uiDialog('setUserParam', 'add');
        dialog.uiDialog('button', 'btnOK').show();
        dialog.uiDialog('button', 'btnSave').hide();
        dialog.uiDialog('open');
    });
    //删除
    $('#btn2').bind('click', function () {
        var ids = grid.uiGrid('selectedData', 'id');
        if (ids.length == 0) {
            showResult(false, '请选择要删除的行！', 'alert');
        } else {
            deleteRow(ids.join(','));
        }
    });
    $('#btn3').bind('click', function () {
        dialogHelp.uiDialog('open');
    });
    //帮助窗口，开发时删除
    var dialogHelp = $('#dialog_help').uiDialog({
        title: '使用帮助',
        panelStyle: 'ui_panel_info',
        icon: 'list',
        width: 600,
        height: 500,
        lazyLoad:true,
        mask: true,
        url: 'help.html',
        iniframe: true
    });
});
//查看
function viewRow(id, title) {
    var dialog = $('#dialog');
    title = '-' + title || '';
    dialog.uiDialog('title').html('查看' + title);
    dialog.uiDialog('setUserParam', 'view');
    dialog.uiDialog('button', 'btnSave').hide();
    dialog.uiDialog('button', 'btnOK').hide();
    dialog.uiDialog('open');
};
//编辑
function editRow(id) {
    var dialog = $('#dialog');
    dialog.uiDialog('title').html('编辑');
    dialog.uiDialog('setUserParam', 'edit');
    dialog.uiDialog('button', 'btnSave').show();
    dialog.uiDialog('button', 'btnOK').hide();
    dialog.uiDialog('open');
};
//删除
function deleteRow(id) {
    var grid = $('#grid');
    $.uiMessagebox.confirm({
        title: '确认操作',
        content: '确定删除选定的数据吗？',
        onClose: function (value) {
            if (value) {
                $.ajax({
                    type: 'post',
                    url: deleteUrl,
                    data: {"id": id},
                    dataType: "json",
                    beforeSend: function () {
                        if (!_IEunder6) {
                            $.uiLoading.show();
                        }
                    },
                    success: function (result) {
                        $.uiLoading.hide();
                        if (result.success) {
                            grid.uiGrid('loadData');
                        }
                        showResult(result.success, result.message);
                    },
                    error: function () {
                        $.uiLoading.hide();
                        showResult(false, '服务器处理失败！', 'error');
                    }
                });
            }
        }
    });
};